package xml;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;
import org.xml.sax.SAXException;


import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;

/**
 * @author 吴阳基
 * @datetime 2024/1/4 11:52
 */
public class ShiLi7 {
    @Test
    public void showPhoneInfo() throws IOException, SAXException, ParserConfigurationException, TransformerException {
        Document doc = null;
        try{
            //加载DOM树
            SAXReader saxReader = new SAXReader();
            doc = saxReader.read(new File("test/xml/shili1.xml"));
        }catch (Exception e){
            e.printStackTrace();
        }
        //获取XML的根节点
        Element root = doc.getRootElement();
        //遍历所有的Brand标签
        for(Iterator itBrand = root.elementIterator(); itBrand.hasNext();){
            Element brand = (Element) itBrand.next();
            //输出标签的name属性
            System.out.println("品牌："+brand.attributeValue("name"));
            //遍历Type标签
            for(Iterator itType = brand.elementIterator();itType.hasNext();) {
                Element type = (Element) itType.next();
                //输出标签的name属性
                System.out.println("\t型号："+type.attributeValue("name"));
            }
        }


    }
}
